package com.sogoodsoft.util;

import java.util.HashMap;
import java.util.Map;

import org.springside.modules.utils.encode.JsonBinder;

/**
 * @author lxz
 *<p>表单处理后 生成dwz框架的 json 消息</p>
 *<p>{
 *	"statusCode":"200", 
 *	"message":"操作成功", 
 *	"navTabId":"navNewsLi", 
 *	"forwardUrl":"", 
 *	"callbackType":"closeCurrent"
 *}</p>

 */
public  class DwzMsg {
	
	/**
	 * 
	 *状态值
	 */
	public enum STATUS{
		SUCCESS(200),		//操作成功
		FAIL(300);			//操作失败
		private final int value;
		private STATUS(int value){
			this.value = value;
		}
		public int getValue(){
			return this.value;
		}
	}
	
	
	public final static String closeCurrent = "closeCurrent";
	public final static String forward = "forward";
	public STATUS statusCode = STATUS.SUCCESS;
	public String message = "操作成功！";
	public String navTabId = "";
	public String forwardUrl = "";
	public String callbackType = "";
	
	/**页面传递的标签名在request里的存储名*/
	public final static String param_navTabId = "navTabId";
	
	
	private static JsonBinder binder = JsonBinder.buildNonDefaultBinder();
	
	/**
	 * 操作成功
	 * @param action
	 */
	public static String success(){
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("statusCode", String.valueOf(STATUS.SUCCESS.getValue()));
		msg.put("message", "操作成功！");
		return binder.toJson(msg);
	}
	
	/**
	 * 操作成功 后回调函数
	 * @param action
	 */
	public static String success(String callbackType){
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("statusCode", String.valueOf(STATUS.SUCCESS.getValue()));
		msg.put("message", "操作成功！");
		msg.put("callbackType", callbackType);
		return binder.toJson(msg);
	}
	
	
	/**
	 * 操作成功且发布关闭当前标签命令 刷新列表
	 * @param navTabId 列表标签ID
	 * @return
	 */
	public static String successAndX(String navTabId){
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("statusCode", String.valueOf(STATUS.SUCCESS.getValue()));
		msg.put("message", "操作成功！");
		msg.put("navTabId", navTabId);
		msg.put("callbackType", closeCurrent);
		return binder.toJson(msg);
	}
	
	/**
	 * 操作成功且发布关闭当前标签命令
	 * @param action
	 */
	public static String successAndX(){
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("statusCode", String.valueOf(STATUS.SUCCESS.getValue()));
		msg.put("message", "操作成功！");
		msg.put("callbackType", closeCurrent);
		return binder.toJson(msg);
	}
	
	public static String successAndReload(String navTabId){
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("statusCode", String.valueOf(STATUS.SUCCESS.getValue()));
		msg.put("message", "操作成功！");
		msg.put("navTabId", navTabId);
		return binder.toJson(msg);
	}
	public static String successAndgo(String forwardUrl){
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("statusCode", String.valueOf(STATUS.SUCCESS.getValue()));
		msg.put("message", "操作成功！");
		msg.put("callbackType", forward);
		msg.put("forwardUrl", forwardUrl);
		return binder.toJson(msg);
	}
	/**
	 * 操作失败
	 * @param action
	 */
	public String fail(){
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("statusCode", String.valueOf(STATUS.FAIL.getValue()));
		msg.put("message", "操作失败！");
		return binder.toJson(msg);
	}
	
	/**
	 * 操作失败 和失败原因
	 * @param action
	 */
	public static String fail(String info){
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("statusCode", String.valueOf(STATUS.FAIL.getValue()));
		msg.put("message", info);
		return binder.toJson(msg);
	}
	
	/**
	 * 自定义dwz消息
	 * @param action
	 */
	public String msg(){
		Map<String, String> msg = new HashMap<String, String>();
		msg.put("statusCode", String.valueOf(statusCode.getValue()));
		msg.put("message", message);
		msg.put("navTabId", navTabId);
		msg.put("forwardUrl", forwardUrl);
		msg.put("callbackType", callbackType);
		return binder.toJson(msg);
	}
	
	
	/**
	 * enum类测试
	 * @param args
	 */
	public static void main(String args[]){
		STATUS s = STATUS.FAIL;
		System.out.print(s.getValue());
	}
}
